<template>
  <header class="header">
    <h1>todos</h1>
    <input v-model="isAll" id="toggle-all" class="toggle-all" type="checkbox" />
    <label for="toggle-all"></label>
    <input
      v-model.trim="todoName"
      @keyup.enter="addTodo"
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
    />
  </header>
</template>

<script>
export default {
  props: {
    list: {
      type: Array,
      default: () => []
    },
  },
  data () {
    return {
      todoName: ''
    }
  },
  computed: {
    isAll: {
      get () {
        return this.list.every(item => item.isDone)
      },
      set (bool) {
        this.list.forEach(item => item.isDone = bool)
      }
    }
  },
  methods: {
    addTodo () {
      if (!this.todoName) {
        return alert('添加的待办事项不能为空')
      }
      this.$emit('add-todo', this.todoName)
      this.todoName = ''
    }
  },
}
</script>